//
//  KYWeatherView.m
//  kuaiyi
//
//  Created by 杨亮 on 16/3/11.
//  Copyright © 2016年 kuaiyi. All rights reserved.
//

#import "KYWeatherView.h"
#import <Masonry.h>
#import "Header.h"
#import "UILabel+KYLabel.h"
#import "KYWeatherView.h"
#import "KYweatherModel.h"
#import <SDWebImageManager.h>

@interface KYWeatherView ()
@property (nonatomic, strong)UIImageView *weatherImage;
@property (nonatomic, strong)UILabel *weatherLabel;

@property (nonatomic, strong)UIImageView *separateLine;
@property (nonatomic, strong)UILabel *temperatureLabel;
@property (nonatomic, strong)UILabel *dataLabel;
@end

@implementation KYWeatherView
#pragma mark - 懒加载
-(UIImageView *)weatherImage{
    if (!_weatherImage) {
        _weatherImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"sun"]];
//        [_weatherImage sizeToFit];
    }
    return _weatherImage;
}
-(UILabel *)weatherLabel{
    if (!_weatherLabel) {
        _weatherLabel = [[UILabel alloc]init];
        _weatherLabel.text = @"晴";
        _weatherLabel.textColor = topTextColor;
//        _weatherLabel.font = [UIFont systemFontOfSize:20];
//        _weatherLabel = [UILabel labelWithHoldText:@"晴"];
    }
    return _weatherLabel;
}
-(UIButton *)placeName{
    if (!_placeName) {
        _placeName = [UIButton buttonWithType:UIButtonTypeCustom];
//        [_placeName setTintColor:topTextColor];
        //要先设置是costom类型的按钮,用set方法赋值
//        _placeName.titleLabel.tintColor = topTextColor;
        [_placeName setTitleColor:topTextColor forState:UIControlStateNormal];
        [_placeName setTitle:@"北京市" forState:UIControlStateNormal];
        [_placeName addTarget:self action:@selector(placeNameBtnKlick) forControlEvents:UIControlEventTouchUpInside];
        [_placeName sizeToFit];
    }
    return _placeName;
}
-(UIImageView *)separateLine{
    if (!_separateLine) {
        _separateLine = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"fengexian"]];
    }
    return _separateLine;
}
-(UILabel *)temperatureLabel{
    if (!_temperatureLabel) {
        _temperatureLabel = [UILabel labelWithHoldText:@"温度:0-7"];
    }
    return _temperatureLabel;
}
-(UILabel *)dataLabel{
    if (!_dataLabel) {
        NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
        formatter.dateFormat = @"yyyy年MM月dd日";
        NSString *date = [formatter stringFromDate:[NSDate date]];
        _dataLabel = [UILabel labelWithHoldText:date];
    }
    return _dataLabel;
}
#pragma mark - 初始化方法
-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor colorWithRed:227/255.0 green:247/255.0 blue:250/255.0 alpha:1];
        [self setUpUI];
    }
    return self;
}
#pragma mark - 设置UI
-(void)setUpUI{
    
    [self addSubview:self.separateLine];
    [self addSubview:self.weatherImage];
    [self addSubview:self.weatherLabel];
    [self addSubview:self.placeName];
    [self addSubview:self.temperatureLabel];
    [self addSubview:self.dataLabel];
    
    [self.weatherImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(10);
        make.centerY.equalTo(self.mas_centerY);
        make.width.equalTo(@40);
        make.height.equalTo(@40);
    }];
    [self.weatherLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.weatherImage.mas_right).offset(10);
        make.centerY.equalTo(self.mas_centerY);
    }];
    [self.placeName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.mas_centerY);
        make.left.equalTo(self.weatherLabel.mas_right).offset(20);
        make.right.equalTo(self.separateLine.mas_left).offset(-5);
    }];
    [self.separateLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.centerY.equalTo(self.mas_centerY);
        make.width.equalTo(@1);
        make.height.equalTo(@51.5);
    }];
    [self.temperatureLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.separateLine.mas_right);
        make.right.equalTo(self.mas_right);
        make.top.equalTo(self.separateLine.mas_top);
    }];
    [self.dataLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.separateLine.mas_right);
        make.right.equalTo(self.mas_right);
        make.bottom.equalTo(self.separateLine.mas_bottom);
    }];

}
#pragma mark - 请求数据
-(void)getWeatherDataWithPlaceName:(NSString *)placeName{
    NSString *url =  @"http://api.map.baidu.com/telematics/v3/weather";
    NSDictionary *paramDict = @{@"location":placeName,@"output":@"json", @"ak" : @"17IvzuqmKrK1cGwNL6VQebF9"};
    [KYweatherModel weatherWithUrl:url parma:paramDict successBlock:^(KYweatherModel *model) {
        self.weatherImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:model.dayPictureUrl]]];
        self.weatherLabel.text = model.weather;
        self.temperatureLabel.text = model.temperature;
    } failureBlock:^(NSError *error) {
        NSLog(@"%@",error.description);
    }];
}
#pragma mark - 城市选择按钮点击
-(void)placeNameBtnKlick{
    NSLog(@"选择城市");
    if ([self.delegate respondsToSelector:@selector(placeBtnKlicked:)]) {
        [self.delegate placeBtnKlicked:self];
    }
}
@end
